package com.atls.eduservice.controller;


import com.atls.commonutils.R;
import com.atls.eduservice.entity.EduCourse;
import com.atls.eduservice.entity.vo.CourseInfoVo;
import com.atls.eduservice.entity.vo.CoursePublishVo;
import com.atls.eduservice.entity.vo.CourseQuery;
import com.atls.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-01-30
 */
@Api("课程简介")
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {


    @Autowired
    private EduCourseService courseService;


    //课程列表
    @GetMapping
    public R getCourseList(){
        List<EduCourse> list = courseService.list(null);
        return R.ok().data("list",list);
    }

    //课程分页查询
    @ApiOperation("课程分页查询")
    @GetMapping("courseList/{current}/{limit}")
    public R courseList(@PathVariable Long current,@PathVariable Long limit){
        //创建page对象
        Page<EduCourse> coursePage=new Page<>(current,limit);

        //实现分页
        courseService.page(coursePage,null);
        //总记录数
        long total = coursePage.getTotal();
        //数据List集合
        List<EduCourse> records = coursePage.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }


    //带条件分页查询
    @ApiOperation("带条件的分页查询")
    @PostMapping("coursePageCondition/{current}/{limit}")
    public R coursePageCondition(@PathVariable Long current,
                                 @PathVariable Long limit,
                                 @RequestBody(required = false) CourseQuery courseQuery){

        Page<EduCourse> coursePage=new Page<>(current,limit);
        courseService.pageQuery(coursePage,courseQuery);
        long total = coursePage.getTotal();
        List<EduCourse> records = coursePage.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }


    @ApiOperation("添加课程信息")
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        String id=courseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",id);
    }

    //根据课程id查询课程信息
    @ApiOperation("根据课程id查询课程信息")
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
            CourseInfoVo courseInfoVo=courseService.getCourseInfo(courseId);
            return R.ok().data("courseInfoVo",courseInfoVo);
    }

    //修改课程信息
    @ApiOperation("修改课程信息")
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
       courseService.updateCourseInfo(courseInfoVo);
       return R.ok();
    }

    //根据课程id查询课程确认信息
    @ApiOperation("根据课程id查询课程确认信息")
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id) {
        CoursePublishVo coursePublishVo = courseService.publishCourseInfo(id);
        return R.ok().data("publishCourse",coursePublishVo);
    }


    //课程最终发布
    //修改课程状态
    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse eduCourse=new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");   //设置课程发布状态
        courseService.updateById(eduCourse);
        return R.ok();
    }


    //删除课程
    @ApiOperation("根据课程id删除课程")
    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        courseService.removeCourse(courseId);
        return R.ok();
    }

}

